﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;
using System.Reflection;
using System.Collections;
using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint.WebPartPages;


namespace myFirstSharePointObjectModel.TreeViewStructure
{
    public partial class TreeViewStructureUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // start off by adding a base treeview node 
            TableRow row = new TableRow();

            TableCell nameCell = new TableCell();
            nameCell.CssClass = "nameFirstColumnStyle";
            TableCell valueCell = new TableCell();
            valueCell.CssClass = "valueFirstColumnStyle";
            nameCell.Text = "Name";
            valueCell.Text = "Value";
            row.Cells.Add(nameCell);
            row.Cells.Add(valueCell);
            propertiesTable.Rows.Add(row);
            if (!IsPostBack)
            {
                TreeNode mainNode = new TreeNode("Sharepoint", "1");
                this.navigationTreeView.Nodes.Add(mainNode);
                SPFarm thisFarm = SPFarm.Local;
                //Iterate through WebApplications
                foreach (SPService svc in thisFarm.Services)
                {
                    if (svc is SPWebService && svc.Name != "WSS_Administration")
                    {
                        SPWebService webSvc = (SPWebService)svc;

                        TreeNode svcNode = new TreeNode();
                        mainNode.ChildNodes.Add(svcNode);
                        processWebService(webSvc, svcNode);
                    }
                }
            }
        }

        protected void navigationTreeView_SelectedNodeChanged(object sender, EventArgs e)
        {
            TreeNode node = ((TreeView)sender).SelectedNode;
            selectedNodeNameLabel.CssClass = "nameLabelStyle";
            selectedNodeTypeLabel.CssClass = "typeLabelStyle";
            selectedNodeNameLabel.Text = node.Text;
            if (kindOfNode(node).ToLower() == "site")
            {
                selectedNodeTypeLabel.Text = "(SPWeb)";
                //get the site based on the GUID
                string siteCollectionId = getSiteCollectionId(node);
                Guid siteID = new Guid(getNodeId(node));
                using (SPSite siteCollection = new SPSite(new Guid(siteCollectionId)))
                {
                    using (SPWeb site = siteCollection.OpenWeb(siteID))
                    {
                        displayProperties(site);
                    }
                }
            }
            else if (kindOfNode(node).ToLower() == "webapplication")
            {
                selectedNodeTypeLabel.Text = "(SPWebApplication)";
                SPFarm thisFarm = SPFarm.Local;
                Guid webAppId = new Guid(getNodeId(node));
                SPWebApplication webApp = (SPWebApplication)thisFarm.GetObject(webAppId);
                displayProperties(webApp);
            }
            /*else if (kindOfNode(node).ToLower() == "contentservice")
            {
                selectedNodeTypeLabel.Text = "(SPWebService)";
                SPFarm thisFarm = SPFarm.Local;
                string webServiceId = getNodeId(node);
                SPWebService webService = (SPWebService)(thisFarm.Services[webServiceId]);
               // Guid webAppId = new Guid(getNodeId(node));
               // SPWebApplication webApp = (SPWebApplication)thisFarm.GetObject(webAppId);
                displayProperties(webService);
            }*/

            else if (kindOfNode(node).ToLower() == "sitecollection")
            {
                selectedNodeTypeLabel.Text = "(SPSite)";
                string siteCollectionId = getSiteCollectionId(node);
                using (SPSite siteCollection = new SPSite(new Guid(siteCollectionId)))
                {
                    displayProperties(siteCollection);
                }
            }
            else if (kindOfNode(node).ToLower() == "list")
            {
                selectedNodeTypeLabel.Text = "(SPList)";
                string listId = getNodeId(node);
                string siteCollectionId = getSiteCollectionId(node);
                string siteId = getNodeId(node.Parent.Parent);
                using (SPSite siteCollection = new SPSite(new Guid(siteCollectionId)))
                {
                    using (SPWeb site = siteCollection.OpenWeb(new Guid(siteId)))
                    {
                        SPList list = site.Lists[new Guid(listId)];
                        displayProperties(list);
                    }
                }
            }
            else if (kindOfNode(node).ToLower() == "feature")
            {
                selectedNodeTypeLabel.Text = "(SPFeature)";
                string featureId = getNodeId(node);
                string siteCollectionId = "";
                string siteId = "";
                string webAppId = "";
                if (kindOfNode(node.Parent.Parent).ToLower() == "webapplication")
                {
                    webAppId = getWebAppId(node);
                    SPWebApplication webApp = (SPWebApplication)SPFarm.Local.GetObject(new Guid(webAppId));
                    SPFeature feature = webApp.Features[new Guid(featureId)];
                    displayProperties(feature);
                }
                else if (kindOfNode(node.Parent.Parent).ToLower() == "sitecollection")
                {
                    siteCollectionId = getSiteCollectionId(node);
                    using (SPSite siteCollection = new SPSite(new Guid(siteCollectionId)))
                    {
                        SPFeature feature = siteCollection.Features[new Guid(featureId)];
                        displayProperties(feature);
                    }
                }
                else if (kindOfNode(node.Parent.Parent).ToLower() == "site")
                {

                    siteCollectionId = getSiteCollectionId(node);
                    siteId = getSiteId(node);
                    using (SPSite siteCollection = new SPSite(new Guid(siteCollectionId)))
                    {
                        using (SPWeb site = siteCollection.OpenWeb(new Guid(siteId)))
                        {
                            SPFeature feature = site.Features[new Guid(featureId)];
                            displayProperties(feature);
                        }
                    }
                }
            }
            else if (kindOfNode(node).ToLower() == "listitem")
            {
                selectedNodeTypeLabel.Text = "(SPListItem)";
                string listItemUniqueId = getNodeId(node);
                string listId = getListId(node);
                string siteCollectionId = getSiteCollectionId(node);
                string siteId = getSiteId(node);
                using (SPSite siteCollection = new SPSite(new Guid(siteCollectionId)))
                {
                    using (SPWeb site = siteCollection.OpenWeb(new Guid(siteId)))
                    {
                        SPList list = site.Lists[new Guid(listId)];
                        SPListItem listItem = list.Items[new Guid(listItemUniqueId)];
                        displayProperties(listItem);
                    }
                }
            }
            else if (kindOfNode(node).ToLower() == "contenttype")
            {
                selectedNodeTypeLabel.Text = "(SPContentType)";
                string contentTypeId = getNodeId(node);
                string siteCollectionId = getSiteCollectionId(node);
                string listId = "";
                string siteId = "";
                if (kindOfNode(node.Parent.Parent).ToLower() == "list")
                {
                    listId = getNodeId(node.Parent.Parent);
                    siteId = getNodeId(node.Parent.Parent.Parent.Parent);
                    using (SPSite siteCollection = new SPSite(new Guid(siteCollectionId)))
                    {
                        using (SPWeb site = siteCollection.OpenWeb(new Guid(siteId)))
                        {
                            SPList list = site.Lists[new Guid(listId)];
                            SPContentType contentType = list.ContentTypes[new SPContentTypeId(contentTypeId)];
                            displayProperties(contentType);
                        }
                    }
                }
                else if (kindOfNode(node.Parent.Parent).ToLower() == "site")
                {
                    siteId = getNodeId(node.Parent.Parent);
                    using (SPSite siteCollection = new SPSite(new Guid(siteCollectionId)))
                    {
                        using (SPWeb site = siteCollection.OpenWeb(new Guid(siteId)))
                        {
                            SPContentType contentType = site.ContentTypes[new SPContentTypeId(contentTypeId)];
                            displayProperties(contentType);
                        }
                    }
                }
            }
            else if (kindOfNode(node).ToLower() == "contenttypefield")
            {
                selectedNodeTypeLabel.Text = "(SPField)";
                string listId = "";
                string siteCollectionId = getSiteCollectionId(node);
                string siteId = getSiteId(node);
                string contentTypeId = getNodeId(node.Parent.Parent);
                string fieldId = getNodeId(node);
                if (kindOfNode(node.Parent.Parent.Parent.Parent).ToLower() == "list")
                {
                    listId = getListId(node);
                    using (SPSite siteCollection = new SPSite(new Guid(siteCollectionId)))
                    {
                        using (SPWeb site = siteCollection.OpenWeb(new Guid(siteId)))
                        {
                            SPList list = site.Lists[new Guid(listId)];
                            SPContentType contentType = list.ContentTypes[new SPContentTypeId(contentTypeId)];
                            SPField field = contentType.Fields[new Guid(fieldId)];
                            displayProperties(field);
                        }
                    }
                }
                else if (kindOfNode(node.Parent.Parent.Parent.Parent).ToLower() == "site")
                {
                    using (SPSite siteCollection = new SPSite(new Guid(siteCollectionId)))
                    {
                        using (SPWeb site = siteCollection.OpenWeb(new Guid(siteId)))
                        {
                            SPContentType contentType = site.ContentTypes[new SPContentTypeId(contentTypeId)];
                            SPField field = contentType.Fields[new Guid(fieldId)];
                            displayProperties(field);
                        }
                    }
                }
            }
            else if (kindOfNode(node).ToLower() == "listfield")
            {
                selectedNodeTypeLabel.Text = "(SPField)";
                string siteId = getSiteId(node);
                string fieldId = getNodeId(node);
                string listId = getListId(node);
                string siteCollectionId = getSiteCollectionId(node);
                using (SPSite siteCollection = new SPSite(new Guid(siteCollectionId)))
                {
                    using (SPWeb site = siteCollection.OpenWeb(new Guid(siteId)))
                    {
                        SPList list = site.Lists[new Guid(listId)];
                        SPField field = list.Fields[new Guid(fieldId)];
                        displayProperties(field);
                    }
                }
            }
            else if (kindOfNode(node).ToLower() == "sitefield")
            {
                selectedNodeTypeLabel.Text = "(SPField)";
                string siteId = getSiteId(node);
                string fieldId = getNodeId(node);
                string siteCollectionId = getSiteCollectionId(node);
                using (SPSite siteCollection = new SPSite(new Guid(siteCollectionId)))
                {
                    using (SPWeb site = siteCollection.OpenWeb(new Guid(siteId)))
                    {
                        SPField field = site.Fields[new Guid(fieldId)];
                        displayProperties(field);
                    }
                }
            }
            else if (kindOfNode(node).ToLower() == "recyclebinitem")
            {
                selectedNodeTypeLabel.Text = "SPRecycleBinItem";
                //get the site based on the GUID
                string siteCollectionId = getSiteCollectionId(node);
                string recycleBinItemId = getNodeId(node);
                if (kindOfNode(node.Parent.Parent).ToLower() == "sitecollection")
                {
                    using (SPSite siteCollection = new SPSite(new Guid(siteCollectionId)))
                    {
                        SPRecycleBinItem recycleBinItem = siteCollection.RecycleBin.GetItemById(new Guid(recycleBinItemId));
                        displayProperties(recycleBinItem);

                    }
                }
                else if (kindOfNode(node.Parent.Parent).ToLower() == "site")
                {
                    string siteId = getSiteId(node);
                    using (SPSite siteCollection = new SPSite(new Guid(siteCollectionId)))
                    {
                        using (SPWeb site = siteCollection.OpenWeb(new Guid(siteId)))
                        {
                            SPRecycleBinItem recycleBinItem = site.RecycleBin.GetItemById(new Guid(recycleBinItemId));
                            displayProperties(recycleBinItem);
                        }
                    }
                }
            }
            else if (kindOfNode(node).ToLower() == "jobdefinition")
            {
                selectedNodeTypeLabel.Text = "(SPJobDefinition)";
                string webAppId = getWebAppId(node);
                string jobDefinitionId = getNodeId(node);
                SPWebApplication webApp = (SPWebApplication)SPFarm.Local.GetObject(new Guid(webAppId));
                SPJobDefinition jobDefinition = webApp.JobDefinitions[new Guid(jobDefinitionId)];
                displayProperties(jobDefinition);
            }
            else if (kindOfNode(node).ToLower() == "runningjob")
            {
                selectedNodeTypeLabel.Text = "(SPRunningJob)";
                string webAppId = getWebAppId(node);
                string runningJobId = getNodeId(node);
                SPWebApplication webApp = (SPWebApplication)SPFarm.Local.GetObject(new Guid(webAppId));
                SPJobDefinition runningJob = webApp.JobDefinitions[new Guid(runningJobId)];
                displayProperties(runningJob);
            }
            else if (kindOfNode(node).ToLower() == "eventreceiver")
            {
                selectedNodeTypeLabel.Text = "(SPEventReceiverDefinition)";
                string eventReceiverId = getNodeId(node);
                string siteCollectionId = getSiteCollectionId(node);
                string siteId = getSiteId(node);
                using (SPSite siteCollection = new SPSite(new Guid(siteCollectionId)))
                {
                    using (SPWeb site = siteCollection.OpenWeb(new Guid(siteId)))
                    {
                        SPEventReceiverDefinition eventReceiver = site.EventReceivers[new Guid(eventReceiverId)];
                        displayProperties(eventReceiver);
                    }
                }
            }
            else if (kindOfNode(node).ToLower() == "user")
            {
                selectedNodeTypeLabel.Text = "(SPUser)";
                string userId = getNodeId(node);
                string siteCollectionId = getSiteCollectionId(node);
                string siteId = getSiteId(node);
                using (SPSite siteCollection = new SPSite(new Guid(siteCollectionId)))
                {
                    using (SPWeb site = siteCollection.OpenWeb(new Guid(siteId)))
                    {
                        SPUser user = site.Users[userId];
                        displayProperties(user);
                    }
                }
            }
            else if (kindOfNode(node).ToLower() == "listtemplate")
            {
                selectedNodeTypeLabel.Text = "(SPListTemplate)";
                string listTemplateId = getNodeId(node);
                string siteCollectionId = getSiteCollectionId(node);
                string siteId = getSiteId(node);
                using (SPSite siteCollection = new SPSite(new Guid(siteCollectionId)))
                {
                    using (SPWeb site = siteCollection.OpenWeb(new Guid(siteId)))
                    {
                        SPListTemplate listTemplate = site.ListTemplates[listTemplateId];
                        displayProperties(listTemplate);
                    }
                }
            }
            else if (kindOfNode(node).ToLower() == "view")
            {
                selectedNodeTypeLabel.Text = "(SPView)";
                string viewId = getNodeId(node);
                string listId = getListId(node);
                string siteCollectionId = getSiteCollectionId(node);
                string siteId = getSiteId(node);
                using (SPSite siteCollection = new SPSite(new Guid(siteCollectionId)))
                {
                    using (SPWeb site = siteCollection.OpenWeb(new Guid(siteId)))
                    {
                        SPList list = site.Lists[new Guid(listId)];
                        SPView view = list.Views[new Guid(viewId)];
                        displayProperties(view);
                    }
                }
            }
            else if (kindOfNode(node).ToLower() == "form")
            {
                selectedNodeTypeLabel.Text = "(SPForm)";
                string formId = getNodeId(node);
                string listId = getListId(node);
                string siteCollectionId = getSiteCollectionId(node);
                string siteId = getSiteId(node);
                using (SPSite siteCollection = new SPSite(new Guid(siteCollectionId)))
                {
                    using (SPWeb site = siteCollection.OpenWeb(new Guid(siteId)))
                    {
                        SPList list = site.Lists[new Guid(listId)];
                        SPForm form = list.Forms[formId];
                        displayProperties(form);
                    }
                }
            }
            else if (kindOfNode(node).ToLower() == "webapplicationapplicationpool")
            {
                selectedNodeTypeLabel.Text = "(SPApplicationPool)";
                string applicationPoolId = getNodeId(node);
                string webAppId = getWebAppId(node);
                SPWebApplication webApp = (SPWebApplication)SPFarm.Local.GetObject(new Guid(webAppId));
                SPApplicationPool applicationPool = webApp.ApplicationPool;
                displayProperties(applicationPool);
            }
            else if (kindOfNode(node).ToLower() == "contentserviceapplicationpool")
            {
                selectedNodeTypeLabel.Text = "(SPApplicationPool)";
                string applicationPoolId = getNodeId(node);
                string contentServiceId = getNodeId(node.Parent.Parent);
                SPWebService webService = (SPWebService)SPFarm.Local.Services[new Guid(contentServiceId)];
                SPApplicationPool applicationPool = webService.ApplicationPools[new Guid(applicationPoolId)];
                displayProperties(applicationPool);
            }
            else if (kindOfNode(node).ToLower() == "property")
            {
                selectedNodeTypeLabel.Text = "Property";
                string value = getNodeId(node);
                string key = node.Text;
                showProperty(key, value);
            }
            else if (kindOfNode(node).ToLower() == "File")
            {
                string FileId = getNodeId(node);
                string siteCollectionId = getSiteCollectionId(node);
                string siteId = getSiteId(node);
                string folderId = getNodeId(node.Parent);
                using (SPSite siteCollection = new SPSite(new Guid(siteCollectionId)))
                {
                    using (SPWeb site = siteCollection.OpenWeb(new Guid(siteId)))
                    {
                        SPFolder folder = site.GetFolder(new Guid(folderId));
                        SPFile File = folder.Files[FileId];
                        displayProperties(File);
                    }
                }
            }
            else if (kindOfNode(node).ToLower() == "quotatemplate")
            {
                selectedNodeTypeLabel.Text = "(SPQuotaTemplate)";
                string quotaTemplateId = getNodeId(node);
                string contentServiceId = getNodeId(node.Parent.Parent);
                SPWebService webService = (SPWebService)SPFarm.Local.Services[new Guid(contentServiceId)];
                SPQuotaTemplate quotaTemplate = webService.QuotaTemplates[node.Text];
                displayProperties(quotaTemplate);
            }
            else if (kindOfNode(node).ToLower() == "iissetting")
            {
                selectedNodeTypeLabel.Text = "IIsSetting";
                string webAppId = getWebAppId(node);
                SPWebApplication webApp = (SPWebApplication)SPFarm.Local.GetObject(new Guid(webAppId));
                System.Collections.Generic.Dictionary<SPUrlZone, SPIisSettings> iisSettingsCollection = webApp.IisSettings;
                string value = getNodeId(node);
                SPUrlZone zone = (SPUrlZone)Enum.Parse(typeof(SPUrlZone), node.Text);
                SPIisSettings iisSettingsObject = iisSettingsCollection[zone];
                displayProperties(iisSettingsObject);
            }
            else if (kindOfNode(node).ToLower() == "webpart")
            {
                selectedNodeTypeLabel.Text = "Webpart";
                string FileId = getNodeId(node.Parent.Parent);
                string siteCollectionId = getSiteCollectionId(node);
                string siteId = getSiteId(node);
                string folderId = getNodeId(node.Parent.Parent.Parent);
                string webPartId = getNodeId(node);
                using (SPSite siteCollection = new SPSite(new Guid(siteCollectionId)))
                {
                    using (SPWeb site = siteCollection.OpenWeb(new Guid(siteId)))
                    {
                        SPFolder folder = site.GetFolder(new Guid(folderId));
                        SPFile File = folder.Files[FileId];
                        System.Web.UI.WebControls.WebParts.WebPart webPart = File.GetLimitedWebPartManager(PersonalizationScope.Shared).WebParts[webPartId];
                        displayProperties(webPart);
                    }
                }
            }
        }

        public static bool ExistsField(SPFieldCollection fieldCollection, string nameOfField)
        {
            bool exists = false;
            foreach (SPField field in fieldCollection)
            {
                if (field.InternalName == nameOfField)
                {
                    exists = true;
                    break;
                }
            }
            return exists;
        }
        protected void addLists(SPListCollection listCollection, TreeNode listCollectionNode)
        {
            listCollectionNode.ChildNodes.Clear();

            foreach (SPList list in listCollection)
            {
                TreeNode fieldsIntermediateNode = new TreeNode("Fields (" + list.Fields.Count + ")", "listfields", "/_layouts/images/fieldimage.png");
                if (list.Fields.Count > 0)
                    fieldsIntermediateNode.ChildNodes.Add(new TreeNode());

                TreeNode workFlowAssociationsIntermediateNode = new TreeNode("WorkFlow Associations (" + list.WorkflowAssociations.Count + ")", "listworkflows", "/_layouts/images/workflowimage.png");
                if (list.WorkflowAssociations.Count > 0)
                    workFlowAssociationsIntermediateNode.ChildNodes.Add(new TreeNode());

                TreeNode contentTypesIntermediateNode = new TreeNode("Content Types (" + list.ContentTypes.Count + ")", "listcontenttypes", "/_layouts/images/fieldimage.png");
                if (list.ContentTypes.Count > 0)
                    contentTypesIntermediateNode.ChildNodes.Add(new TreeNode());

                TreeNode eventReceiversIntermediateNode = new TreeNode("Event Receivers (" + list.EventReceivers.Count + ")", "listeventreceivers", "/_layouts/images/eventimage.png");
                if (list.EventReceivers.Count > 0)
                    eventReceiversIntermediateNode.ChildNodes.Add(new TreeNode());

                TreeNode formsIntermediateNode = new TreeNode("Forms (" + list.Forms.Count + ")", "listforms", "/_layouts/images/formimage.png");
                if (list.Forms.Count > 0)
                    formsIntermediateNode.ChildNodes.Add(new TreeNode());

                TreeNode itemsIntermediateNode = new TreeNode("Items (" + list.Items.Count + ")", "listitems", "/_layouts/images/itemimage.png");
                if (list.Items.Count > 0)
                    itemsIntermediateNode.ChildNodes.Add(new TreeNode());

                TreeNode viewsIntermediateNode = new TreeNode("Views (" + list.Views.Count + ")", "listviews", "/_layouts/images/listimage.png");
                if (list.Views.Count > 0)
                    viewsIntermediateNode.ChildNodes.Add(new TreeNode());

                int nrChildFields = list.Fields.Count;
                TreeNode listNode = new TreeNode(list.Title + " (" + nrChildFields + ")", "list;" + list.ID.ToString(), "/_layouts/images/listimage.png");
                listNode.ChildNodes.Add(fieldsIntermediateNode);
                listNode.ChildNodes.Add(workFlowAssociationsIntermediateNode);
                listNode.ChildNodes.Add(viewsIntermediateNode);
                listNode.ChildNodes.Add(itemsIntermediateNode);
                listNode.ChildNodes.Add(formsIntermediateNode);
                listNode.ChildNodes.Add(contentTypesIntermediateNode);
                listNode.ChildNodes.Add(eventReceiversIntermediateNode);
                listCollectionNode.ChildNodes.Add(listNode);
            }
        }
        public void addFeatures(SPFeatureCollection featureCollection, TreeNode featureCollectionNode)
        {
            featureCollectionNode.ChildNodes.Clear();
            foreach (SPFeature feature in featureCollection)
            {
                TreeNode featureNode = new TreeNode(feature.Definition.DisplayName, "feature;" + feature.Definition.Id.ToString(), "/_layouts/images/featureimage.png");
                featureCollectionNode.ChildNodes.Add(featureNode);
            }
        }
        public void addFolder(SPFolder folder, TreeNode folderNode)
        {
            folderNode.ChildNodes.Clear();
            TreeNode propertiesNode = new TreeNode("Properties (" + folder.Properties.Count.ToString() + ")", "folderproperties;", "/_layouts/images/propertyimage.png");
            propertiesNode.ChildNodes.Add(new TreeNode());
            folderNode.ChildNodes.Add(propertiesNode);
            addFiles(folder.Files, folderNode);
            foreach (SPFolder subfolder in folder.SubFolders)
            {
                TreeNode subfolderNode = new TreeNode(subfolder.Name, "folder;" + subfolder.UniqueId.ToString(), "/_layouts/images/folderimage.png");
                subfolderNode.ChildNodes.Add(new TreeNode());
                folderNode.ChildNodes.Add(subfolderNode);
            }
        }
        public void addFiles(SPFileCollection FileCollection, TreeNode FileCollectionNode)
        {
            foreach (SPFile File in FileCollection)
            {
                TreeNode FileNode = new TreeNode(File.Name, "File;" + File.Url, "/_layouts/images/Fileimage.png");
                TreeNode webPartsNode = new TreeNode("WebParts", "webparts;", "/_layouts/images/listimage.png");
                TreeNode eventReceiversNode = new TreeNode("Event Receivers(" + File.EventReceivers.Count.ToString() + ")", "Fileeventreceivers;", "/_layouts/images/eventimage.png");
                if (File.EventReceivers.Count > 0)
                    eventReceiversNode.ChildNodes.Add(new TreeNode());
                TreeNode propertiesNode = new TreeNode("Properties (" + File.Properties.Count.ToString() + ")", "Fileproperties;", "/_layouts/images/propertyimage.png");
                if (File.Properties.Count > 0)
                    propertiesNode.ChildNodes.Add(new TreeNode());
                webPartsNode.ChildNodes.Add(new TreeNode());
                FileNode.ChildNodes.Add(webPartsNode);
                FileNode.ChildNodes.Add(eventReceiversNode);
                FileNode.ChildNodes.Add(propertiesNode);
                if (File.Url.Split('.')[1] == "aspx")
                {
                    SPLimitedWebPartManager manager = File.GetLimitedWebPartManager(PersonalizationScope.Shared);

                    if (manager != null)
                    {
                        webPartsNode.ChildNodes.Clear();
                        foreach (System.Web.UI.WebControls.WebParts.WebPart webPart in manager.WebParts)
                        {
                            TreeNode webPartNode = new TreeNode(webPart.Title, "webpart;" + webPart.UniqueID, "/_layouts/images/listimage.png");
                            webPartsNode.ChildNodes.Add(webPartNode);
                        }

                    }
                }
                FileCollectionNode.ChildNodes.Add(FileNode);
            }
        }
        protected void ProcessSite(SPWeb site, TreeNode siteNode)
        {

            siteNode.ChildNodes.Clear();
            //child collections declaration
            SPFeatureCollection featureCollection = site.Features;
            SPContentTypeCollection contentTypeCollection = site.ContentTypes;
            SPEventReceiverDefinitionCollection eventReceiverCollection = site.EventReceivers;
            SPFieldCollection fieldCollection = site.Fields;
            SPListTemplateCollection listTemplateCollection = site.ListTemplates;
            SPListCollection listCollection = site.Lists;
            Hashtable propertiesTable = site.AllProperties;
            SPRecycleBinItemCollection recycleBin = site.RecycleBin;
            SPFolder rootFolder = site.RootFolder;
            SPUserCollection userCollection = site.Users;
            SPWebCollection webCollection = site.Webs;
            //intermediate treenodes declaration
            TreeNode featureCollectionNode = new TreeNode("Site Features (" + featureCollection.Count + ")", "siteFeatures;", "/_layouts/images/featureimage.png");
            TreeNode contentTypeCollectionNode = new TreeNode("Content Types (" + contentTypeCollection.Count + ")", "sitecontentTypes;", "/_layouts/images/fieldimage.png");
            TreeNode propertiesTableNode = new TreeNode("Properties (" + propertiesTable.Count + ")", "siteProperties;", "/_layouts/images/propertyimage.png");
            TreeNode eventReceiverCollectionNode = new TreeNode("Event Receivers (" + eventReceiverCollection.Count + ")", "siteeventReceivers;", "/_layouts/images/eventimage.png");
            TreeNode fieldCollectionNode = new TreeNode("Fields (" + fieldCollection.Count + ")", "sitefields;", "/_layouts/images/fieldimage.png");
            TreeNode listTemplateCollectionNode = new TreeNode("List Templates (" + listTemplateCollection.Count + ")", "listTemplates;", "/_layouts/images/listimage.png");
            TreeNode listCollectionNode = new TreeNode("Lists (" + listCollection.Count + ")", "lists;", "/_layouts/images/listimage.png");
            TreeNode rootFolderNode = new TreeNode("Root Folder", "folder;" + site.RootFolder.UniqueId.ToString(), "/_layouts/images/folderimage.png");
            TreeNode recycleBinNode = new TreeNode("Recycle Bin (" + recycleBin.Count + ")", "siterecyclebin;", "/_layouts/images/binimage.png");
            TreeNode userCollectionNode = new TreeNode("Users (" + userCollection.Count + ")", "users;", "/_layouts/images/userimage.png");
            TreeNode webCollectionNode = new TreeNode("Webs (" + webCollection.Count + ")", "webs;", "/_layouts/images/siteimage.png");

            //adding intermediate nodes

            siteNode.ChildNodes.Add(rootFolderNode);
            rootFolderNode.ChildNodes.Add(new TreeNode());
            siteNode.ChildNodes.Add(contentTypeCollectionNode);
            if (contentTypeCollection.Count != 0)
                contentTypeCollectionNode.ChildNodes.Add(new TreeNode());
            siteNode.ChildNodes.Add(eventReceiverCollectionNode);
            if (eventReceiverCollection.Count != 0)
                eventReceiverCollectionNode.ChildNodes.Add(new TreeNode());
            siteNode.ChildNodes.Add(propertiesTableNode);
            if (propertiesTable.Count != 0)
                propertiesTableNode.ChildNodes.Add(new TreeNode());
            siteNode.ChildNodes.Add(featureCollectionNode);
            if (featureCollection.Count != 0)
                featureCollectionNode.ChildNodes.Add(new TreeNode());
            siteNode.ChildNodes.Add(fieldCollectionNode);
            if (fieldCollection.Count != 0)
                fieldCollectionNode.ChildNodes.Add(new TreeNode());
            siteNode.ChildNodes.Add(listTemplateCollectionNode);
            if (listTemplateCollection.Count != 0)
                listTemplateCollectionNode.ChildNodes.Add(new TreeNode());
            siteNode.ChildNodes.Add(listCollectionNode);
            if (listCollection.Count != 0)
                listCollectionNode.ChildNodes.Add(new TreeNode());
            siteNode.ChildNodes.Add(recycleBinNode);
            if (recycleBin.Count != 0)
                recycleBinNode.ChildNodes.Add(new TreeNode());
            siteNode.ChildNodes.Add(userCollectionNode);
            if (userCollection.Count != 0)
                userCollectionNode.ChildNodes.Add(new TreeNode());
            siteNode.ChildNodes.Add(webCollectionNode);
            if (webCollection.Count != 0)
                webCollectionNode.ChildNodes.Add(new TreeNode());


        }
        protected void processWebService(SPWebService webService, TreeNode webServiceNode)
        {
            //processing the webServiceNode
            webServiceNode.Text = "Content Service";
            webServiceNode.Value = "ContentService;" + webService.Id.ToString();
            webServiceNode.ImageUrl = "/_layouts/images/featureimage.png";

            //child collections declaration
            SPApplicationPoolCollection applicationPoolCollection = webService.ApplicationPools;
            SPQuotaTemplateCollection quotaTemplateCollection = webService.QuotaTemplates;
            Hashtable propertiesTable = webService.Properties;
            SPWebApplicationCollection webApplicationCollection = webService.WebApplications;
            //intermediate treenodes declaration
            TreeNode applicationPoolCollectionNode = new TreeNode("Application Pools (" + applicationPoolCollection.Count + ")", "ApplicationPools;", "/_layouts/images/applicationpoolimage.png");
            TreeNode quotaTemplateCollectionNode = new TreeNode("Quota Templates (" + quotaTemplateCollection.Count + ")", "QuotaTemplates;", "/_layouts/images/quotaTemplateimage.png");
            TreeNode propertiesTableNode = new TreeNode("Properties (" + propertiesTable.Count + ")", "webserviceProperties;", "/_layouts/images/propertyimage.png");
            TreeNode webApplicationCollectionNode = new TreeNode("Web Applications (" + webApplicationCollection.Count + ")", "WebApplications;", "/_layouts/images/webappimage.png");
            //adding intermediate nodes


            webServiceNode.ChildNodes.Add(applicationPoolCollectionNode);
            if (applicationPoolCollection.Count != 0)
                applicationPoolCollectionNode.ChildNodes.Add(new TreeNode());
            webServiceNode.ChildNodes.Add(webApplicationCollectionNode);
            if (webApplicationCollection.Count != 0)
                webApplicationCollectionNode.ChildNodes.Add(new TreeNode());
            webServiceNode.ChildNodes.Add(propertiesTableNode);
            if (propertiesTable.Count != 0)
                propertiesTableNode.ChildNodes.Add(new TreeNode());
            webServiceNode.ChildNodes.Add(quotaTemplateCollectionNode);
            if (quotaTemplateCollection.Count != 0)
                quotaTemplateCollectionNode.ChildNodes.Add(new TreeNode());
        }
        protected void processWebApplication(SPWebApplication webApplication, TreeNode webApplicationNode)
        {
            webApplicationNode.ChildNodes.Clear();
            //child collections declaration
            SPFeatureCollection featureCollection = webApplication.Features;
            SPRunningJobCollection runningJobCollection = webApplication.RunningJobs;
            SPJobDefinitionCollection jobDefinitionCollection = webApplication.JobDefinitions;
            SPSiteCollection siteCollectionCollection = webApplication.Sites;
            Hashtable propertiesTable = webApplication.Properties;
            System.Collections.Generic.Dictionary<SPUrlZone, SPIisSettings> iisSettingsCollection = webApplication.IisSettings;
            addApplicationPool(webApplication.ApplicationPool, webApplicationNode);
            //intermediate treenodes declaration
            TreeNode featureCollectionNode = new TreeNode("Webapplication Features (" + featureCollection.Count + ")", "webappFeatures;", "/_layouts/images/featureimage.png");
            TreeNode runningJobCollectionNode = new TreeNode("Running Jobs (" + runningJobCollection.Count + ")", "RunningJobs;", "/_layouts/images/runningjobimage.png");
            TreeNode propertiesTableNode = new TreeNode("Properties (" + propertiesTable.Count + ")", "webappProperties;", "/_layouts/images/propertyimage.png");
            TreeNode jobDefinitionCollectionNode = new TreeNode("Job Definitions (" + jobDefinitionCollection.Count + ")", "Jobdefinitions;", "/_layouts/images/jobdefinitionimage.png");
            TreeNode siteCollectionCollectionNode = new TreeNode("Site Collections (" + siteCollectionCollection.Count + ")", "sitecollections;", "/_layouts/images/sitecollectionimage.png");
            TreeNode iisSettingCollectionNode = new TreeNode("IIS Settings (" + iisSettingsCollection.Count + ")", "iissettings;", "/_layouts/images/settingimage.png");

            //adding intermediate nodes

            webApplicationNode.ChildNodes.Add(jobDefinitionCollectionNode);
            if (jobDefinitionCollection.Count != 0)
                jobDefinitionCollectionNode.ChildNodes.Add(new TreeNode());
            webApplicationNode.ChildNodes.Add(runningJobCollectionNode);
            if (runningJobCollection.Count != 0)
                runningJobCollectionNode.ChildNodes.Add(new TreeNode());
            webApplicationNode.ChildNodes.Add(propertiesTableNode);
            if (propertiesTable.Count != 0)
                propertiesTableNode.ChildNodes.Add(new TreeNode());
            webApplicationNode.ChildNodes.Add(featureCollectionNode);
            if (featureCollection.Count != 0)
                featureCollectionNode.ChildNodes.Add(new TreeNode());
            webApplicationNode.ChildNodes.Add(iisSettingCollectionNode);
            if (iisSettingsCollection.Count != 0)
                iisSettingCollectionNode.ChildNodes.Add(new TreeNode());
            webApplicationNode.ChildNodes.Add(siteCollectionCollectionNode);
            if (siteCollectionCollection.Count != 0)
                siteCollectionCollectionNode.ChildNodes.Add(new TreeNode());
        }
        protected void processSiteCollection(SPSite siteCollection, TreeNode siteCollectionNode)
        {
            siteCollectionNode.ChildNodes.Clear();
            //child collections declaration
            SPRecycleBinItemCollection recycleBin = siteCollection.RecycleBin;
            SPFeatureCollection featureCollection = siteCollection.Features;
            SPUserSolutionCollection solutionCollection = siteCollection.Solutions;
            SPWeb topLevelSite = siteCollection.RootWeb;

            //intermediate treenodes declaration
            TreeNode featureCollectionNode = new TreeNode("Site Features (" + featureCollection.Count + ")", "sitecolFeatures;", "/_layouts/images/featureimage.png");
            TreeNode recycleBinNode = new TreeNode("Recycle Bin (" + recycleBin.Count + ")", "sitecolRecyclebin;", "/_layouts/images/binimage.png");
            TreeNode solutionCollectionNode = new TreeNode("Solution Collection (" + solutionCollection.Count + ")", "solutions;", "/_layouts/images/solutionimage.png");
            TreeNode topLevelSiteNode = new TreeNode(topLevelSite.Title, "site;" + topLevelSite.ID.ToString(), "/_layouts/images/siteimage.png");



            //adding intermediate nodes

            siteCollectionNode.ChildNodes.Add(recycleBinNode);
            if (recycleBin.Count != 0)
                recycleBinNode.ChildNodes.Add(new TreeNode());
            siteCollectionNode.ChildNodes.Add(solutionCollectionNode);
            if (solutionCollection.Count != 0)
                solutionCollectionNode.ChildNodes.Add(new TreeNode());
            siteCollectionNode.ChildNodes.Add(featureCollectionNode);
            if (featureCollection.Count != 0)
                featureCollectionNode.ChildNodes.Add(new TreeNode());
            siteCollectionNode.ChildNodes.Add(topLevelSiteNode);
            topLevelSiteNode.ChildNodes.Add(new TreeNode());

        }
        public void addContentTypes(SPContentTypeCollection contentTypeCollection, TreeNode contentTypeCollectionNode)
        {
            contentTypeCollectionNode.ChildNodes.Clear();

            foreach (SPContentType contentType in contentTypeCollection)
            {
                TreeNode fieldsIntermediateNode = new TreeNode("Fields (" + contentType.Fields.Count + ")", "contenttypefields", "/_layouts/images/fieldimage.png");
                fieldsIntermediateNode.ChildNodes.Add(new TreeNode());
                TreeNode workFlowAssociationsIntermediateNode = new TreeNode("WorkFlow Associations (" + contentType.WorkflowAssociations.Count + ")", "contenttypeworkflows", "/_layouts/images/workflowimage.png");
                workFlowAssociationsIntermediateNode.ChildNodes.Add(new TreeNode());
                int nrChildFields = contentType.Fields.Count;
                TreeNode contentTypeNode = new TreeNode(contentType.Name + " (" + nrChildFields + ")", "contenttype;" + contentType.Id.ToString(), "/_layouts/images/fieldimage.png");
                contentTypeNode.ChildNodes.Add(fieldsIntermediateNode);
                contentTypeNode.ChildNodes.Add(workFlowAssociationsIntermediateNode);
                contentTypeCollectionNode.ChildNodes.Add(contentTypeNode);
                //addContentTypeFields(contentType.Fields, contentTypeNode);
            }
        }
        public void addWorkFlowAssociations(SPWorkflowAssociationCollection workFlowAssociationCollection, TreeNode workFlowAssociationCollectionNode)
        {
            workFlowAssociationCollectionNode.ChildNodes.Clear();
            foreach (SPWorkflowAssociation workFlowAssociation in workFlowAssociationCollection)
            {
                TreeNode workFlowAssociationNode;
                workFlowAssociationNode = new TreeNode(workFlowAssociation.Name, "workFlowAssociation;" + workFlowAssociation.Id.ToString(), "/_layouts/images/workflowimage.png");
                workFlowAssociationCollectionNode.ChildNodes.Add(workFlowAssociationNode);
            }
        }
        public void addRecycleBin(SPRecycleBinItemCollection recycleBinItemCollection, TreeNode recycleBinNode)
        {
            recycleBinNode.ChildNodes.Clear();
            foreach (SPRecycleBinItem recycleBinItem in recycleBinItemCollection)
            {
                TreeNode recycleBinItemNode = new TreeNode(recycleBinItem.Title, "recyclebinitem;" + recycleBinItem.ID.ToString(), "/_layouts/images/fieldimage.png");
                recycleBinNode.ChildNodes.Add(recycleBinItemNode);
            }
        }
        public void addContentTypeFields(SPFieldCollection fieldCollection, TreeNode fieldCollectionNode)
        {
            fieldCollectionNode.ChildNodes.Clear();
            foreach (SPField field in fieldCollection)
            {
                TreeNode fieldNode;
                fieldNode = new TreeNode(field.InternalName, "contenttypefield;" + field.Id.ToString(), "/_layouts/images/fieldimage.png");
                fieldCollectionNode.ChildNodes.Add(fieldNode);
            }

        }
        public void addSiteFields(SPFieldCollection fieldCollection, TreeNode fieldCollectionNode)
        {
            fieldCollectionNode.ChildNodes.Clear();
            foreach (SPField field in fieldCollection)
            {
                TreeNode fieldNode;
                fieldNode = new TreeNode(field.InternalName, "sitefield;" + field.Id.ToString(), "/_layouts/images/fieldimage.png");
                fieldCollectionNode.ChildNodes.Add(fieldNode);
            }
        }
        public void addListFields(SPFieldCollection fieldCollection, TreeNode fieldCollectionNode)
        {
            fieldCollectionNode.ChildNodes.Clear();
            foreach (SPField field in fieldCollection)
            {
                TreeNode fieldNode;
                fieldNode = new TreeNode(field.InternalName, "listfield;" + field.Id.ToString(), "/_layouts/images/fieldimage.png");
                fieldCollectionNode.ChildNodes.Add(fieldNode);
            }
        }
        public void addWebs(SPWebCollection webCollection, TreeNode webCollectionNode)
        {
            webCollectionNode.ChildNodes.Clear();
            foreach (SPWeb web in webCollection)
            {
                TreeNode webNode;
                webNode = new TreeNode(web.Title, "site;" + web.ID.ToString(), "/_layouts/images/fieldimage.png");
                webNode.ChildNodes.Add(new TreeNode());
                webCollectionNode.ChildNodes.Add(webNode);
            }
        }

        public void addJobDefinitions(SPJobDefinitionCollection jobDefinitionCollection, TreeNode jobDefinitionCollectionNode)
        {
            jobDefinitionCollectionNode.ChildNodes.Clear();
            foreach (SPJobDefinition jobDefinition in jobDefinitionCollection)
            {
                TreeNode jobDefinitionNode = new TreeNode(jobDefinition.Title, "jobdefinition;" + jobDefinition.Id.ToString(), "/_layouts/images/jobdefinitionimage.png");
                jobDefinitionCollectionNode.ChildNodes.Add(jobDefinitionNode);
            }
        }
        public void addRunningJobs(SPRunningJobCollection runningJobsCollection, TreeNode runningJobCollectionNode)
        {
            runningJobCollectionNode.ChildNodes.Clear();
            foreach (SPRunningJob runningJob in runningJobsCollection)
            {
                TreeNode runningJobNode = new TreeNode(runningJob.JobDefinitionTitle, "runningjob;" + runningJob.JobDefinitionId, "/_layouts/images/runningjobimage.png");
                runningJobCollectionNode.ChildNodes.Add(runningJobNode);
            }
        }
        public void addIisSettings(System.Collections.Generic.Dictionary<SPUrlZone, SPIisSettings> iisSettingsCollection, TreeNode iisSettingCollectionNode)
        {
            iisSettingCollectionNode.ChildNodes.Clear();
            foreach (SPUrlZone key in iisSettingsCollection.Keys)
            {
                TreeNode iisSettingNode = new TreeNode(key.ToString(), "iissetting;" + iisSettingsCollection[key].ToString(), "/_layouts/images/settingimage.png");
                iisSettingCollectionNode.ChildNodes.Add(iisSettingNode);
            }
        }
        public void addProperties(Hashtable propertiesTable, TreeNode propertiesTableNode)
        {
            propertiesTableNode.ChildNodes.Clear();
            IDictionaryEnumerator en = propertiesTable.GetEnumerator();
            foreach (string key in propertiesTable.Keys)
            {
                TreeNode propertyNode = new TreeNode(key, "property;" + propertiesTable[key].ToString(), "/_layouts/images/propertyimage.png");
                propertiesTableNode.ChildNodes.Add(propertyNode);
            }
        }
        public void addSolutions(SPUserSolutionCollection solutionCollection, TreeNode solutionCollectionNode)
        {
            solutionCollectionNode.ChildNodes.Clear();
            foreach (SPSolution solution in solutionCollection)
            {
                TreeNode solutionNode = new TreeNode(solution.Name, "solution;" + solution.Id, "/_layouts/images/solutionimage.png");
                solutionCollectionNode.ChildNodes.Add(solutionNode);
            }
        }
        public void addEventReceivers(SPEventReceiverDefinitionCollection eventReceiverCollection, TreeNode eventReceiverCollectionNode)
        {
            eventReceiverCollectionNode.ChildNodes.Clear();
            foreach (SPEventReceiverDefinition eventReceiver in eventReceiverCollection)
            {
                TreeNode eventReceiverNode = new TreeNode(eventReceiver.Name, "eventreceiver;" + eventReceiver.Id.ToString(), "/_layouts/images/eventimage.png");
                eventReceiverCollectionNode.ChildNodes.Add(eventReceiverNode);
            }
        }
        public void addListTemplates(SPListTemplateCollection listTemplateCollection, TreeNode listTemplateCollectionNode)
        {
            listTemplateCollectionNode.ChildNodes.Clear();
            foreach (SPListTemplate listTemplate in listTemplateCollection)
            {
                TreeNode listTemplateNode = new TreeNode(listTemplate.Name, "listtemplate;" + listTemplate.Name, "/_layouts/images/listimage.png");
                listTemplateCollectionNode.ChildNodes.Add(listTemplateNode);
            }
        }
        public void addUsers(SPUserCollection userCollection, TreeNode userCollectionNode)
        {
            userCollectionNode.ChildNodes.Clear();
            foreach (SPUser user in userCollection)
            {
                TreeNode userNode = new TreeNode(user.Name, "user;" + user.LoginName, "/_layouts/images/userimage.png");
                userCollectionNode.ChildNodes.Add(userNode);
            }
        }
        public void addlistItems(SPListItemCollection listItemCollection, TreeNode listItemCollectionNode)
        {
            listItemCollectionNode.ChildNodes.Clear();
            foreach (SPListItem listItem in listItemCollection)
            {
                TreeNode userNode = new TreeNode(listItem.Name, "listitem;" + listItem.UniqueId, "/_layouts/images/itemimage.png");
                listItemCollectionNode.ChildNodes.Add(userNode);
            }
        }
        public void addViews(SPViewCollection viewCollection, TreeNode viewCollectionNode)
        {
            viewCollectionNode.ChildNodes.Clear();
            foreach (SPView view in viewCollection)
            {
                string title = "no name";
                if (view.Title != "")
                {
                    title = view.Title;
                }
                TreeNode viewNode = new TreeNode(title, "view;" + view.ID, "/_layouts/images/listimage.png");
                viewCollectionNode.ChildNodes.Add(viewNode);
            }
        }
        public void addForms(SPFormCollection formCollection, TreeNode formCollectionNode)
        {
            if (formCollection != null)
            {
                formCollectionNode.ChildNodes.Clear();
                foreach (SPForm form in formCollection)
                {
                    try
                    {
                        if (form.ID != null)
                        {
                            TreeNode formNode = new TreeNode(form.Url, "form;" + form.Url, "/_layouts/images/formimage.png");
                            formCollectionNode.ChildNodes.Add(formNode);
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }
        }
        public void addApplicationPools(SPApplicationPoolCollection applicationPoolCollection, TreeNode applicationPoolCollectionNode)
        {
            applicationPoolCollectionNode.ChildNodes.Clear();
            foreach (SPApplicationPool applicationPool in applicationPoolCollection)
            {
                if (applicationPool.Id != null)
                {
                    TreeNode applicationPoolNode = new TreeNode(applicationPool.Name, "contentserviceapplicationpool;" + applicationPool.Id.ToString(), "/_layouts/images/applicationpoolimage.png");
                    applicationPoolCollectionNode.ChildNodes.Add(applicationPoolNode);
                }
            }
        }
        public void addSiteCollections(SPSiteCollection siteCollectionCollection, TreeNode siteCollectionCollectionNode)
        {
            siteCollectionCollectionNode.ChildNodes.Clear();
            foreach (SPSite siteCollection in siteCollectionCollection)
            {
                if (siteCollection.ID != null)
                {
                    TreeNode siteCollectionNode = new TreeNode(siteCollection.Url, "sitecollection;" + siteCollection.ID.ToString(), "/_layouts/images/sitecollectionimage.png");
                    siteCollectionNode.ChildNodes.Add(new TreeNode());
                    siteCollectionCollectionNode.ChildNodes.Add(siteCollectionNode);
                }
            }
        }

        public void addWebApplications(SPWebApplicationCollection webApplicationCollection, TreeNode webApplicationCollectionNode)
        {

            webApplicationCollectionNode.ChildNodes.Clear();
            foreach (SPWebApplication webApplication in webApplicationCollection)
            {
                if (!webApplication.IsAdministrationWebApplication)
                {
                    if (webApplication.Id != null)
                    {
                        TreeNode webApplicationNode = new TreeNode(webApplication.Name, "webapplication;" + webApplication.Id.ToString(), "/_layouts/images/webappimage.png");
                        webApplicationNode.ChildNodes.Add(new TreeNode());
                        webApplicationCollectionNode.ChildNodes.Add(webApplicationNode);
                    }
                }
            }
        }

        public void addApplicationPool(SPApplicationPool applicationPool, TreeNode node)
        {
            TreeNode applicationPoolNode = new TreeNode(applicationPool.Name, "webapplicationapplicationpool;" + applicationPool.Id.ToString(), "/_layouts/images/applicationpoolimage.png");
            node.ChildNodes.Add(applicationPoolNode);
        }
        public void addQuotaTemplates(SPQuotaTemplateCollection quotaTemplateCollection, TreeNode quotaTemplateCollectionNode)
        {

            quotaTemplateCollectionNode.ChildNodes.Clear();
            foreach (SPQuotaTemplate quotaTemplate in quotaTemplateCollection)
            {
                if (quotaTemplate.QuotaID != null)
                {
                    TreeNode quotaTemplateNode = new TreeNode(quotaTemplate.Name, "quotatemplate;" + quotaTemplate.QuotaID.ToString(), "/_layouts/images/quotatemplateimage.png");
                    quotaTemplateCollectionNode.ChildNodes.Add(quotaTemplateNode);
                }
            }
        }
        public string kindOfNode(TreeNode node)
        {
            return (node.Value.Split(';')[0]);
        }
        public void displayProperties(Object sharePointObject)
        {
            Type type = sharePointObject.GetType();
            foreach (PropertyInfo property in type.GetProperties())
            {
                if (property.CanRead && property.PropertyType.GetInterface("IEnumerable", true) == null && property.PropertyType.GetInterface("IList", true) == null)
                {
                    string propertyValue = "";
                    if (property.PropertyType.Name == "String")
                    {
                        propertyValue = (string)property.GetValue(sharePointObject, null);
                    }
                    else if (property.PropertyType.Name.ToLower().Substring(0, 3) == "int")
                    {
                        int propertyValueInt;
                        propertyValueInt = System.Convert.ToInt32(property.GetValue(sharePointObject, null));
                        propertyValue = propertyValueInt.ToString();
                    }
                    else if (property.PropertyType.Name == "DateTime")
                    {
                        try
                        {
                            DateTime propertyValueDateTime = (DateTime)property.GetValue(sharePointObject, null);
                            propertyValue = propertyValueDateTime.ToLongDateString();
                        }
                        catch
                        {
                        }
                    }
                    else
                    {
                        try
                        {
                            Object propertyValueObject = property.GetValue(sharePointObject, null);
                            if (propertyValueObject != null)
                            {
                                propertyValue = propertyValueObject.ToString();
                            }
                        }
                        catch
                        {
                        }
                    }
                    showProperty(property.Name, propertyValue);
                }
            }
        }
        public string getSiteCollectionId(TreeNode node)
        {
            if (node.Value.Split(';')[0].ToLower() == "sitecollection")
            {
                return node.Value.Split(';')[1];
            }
            else
            {
                if (node.Parent != null)
                    return getSiteCollectionId(node.Parent);
                else
                    return "";
            }
        }
        public string getSiteId(TreeNode node)
        {
            if (node.Value.Split(';')[0].ToLower() == "site")
            {
                return node.Value.Split(';')[1];
            }
            else
            {
                if (node.Parent != null)
                    return getSiteId(node.Parent);
                else
                    return "";
            }
        }
        public string getListId(TreeNode node)
        {
            if (node.Value.Split(';')[0].ToLower() == "list")
            {
                return node.Value.Split(';')[1];
            }
            else
            {
                if (node.Parent != null)
                    return getListId(node.Parent);
                else
                    return "";
            }
        }
        public string getContentTypeId(TreeNode node)
        {
            if (node.Value.Split(';')[0].ToLower() == "contenttype")
            {
                return node.Value.Split(';')[1];
            }
            else
            {
                if (node.Parent != null)
                    return getContentTypeId(node.Parent);
                else
                    return "";
            }
        }
        public string getWebAppId(TreeNode node)
        {
            if (node.Value.Split(';')[0].ToLower() == "webapplication")
            {
                return node.Value.Split(';')[1];
            }
            else
            {
                if (node.Parent != null)
                    return getWebAppId(node.Parent);
                else
                    return "";
            }
        }
        public string getNodeId(TreeNode node)
        {
            string[] value = node.Value.Split(';');
            string ID = "";
            if (value.Length == 2)
            {
                ID = value[1];
            }
            return ID;
        }
        public void showProperty(string propertyName, string propertyValue)
        {
            TableRow row = new TableRow();
            TableCell propertyNameCell = new TableCell();
            propertyNameCell.CssClass = "nameColumnStyle";
            TableCell propertyValueCell = new TableCell();
            propertyValueCell.CssClass = "valueColumnStyle";
            propertyNameCell.Text = propertyName;
            propertyValueCell.Text = propertyValue;
            row.Cells.Add(propertyNameCell);
            row.Cells.Add(propertyValueCell);
            propertiesTable.Rows.Add(row);
        }
        protected void navigationTreeView_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
        {
            if (Page.IsPostBack)
            {
                TreeNode expandedNode = e.Node;
                if (kindOfNode(expandedNode).ToLower() == "applicationpools")
                {
                    string contentServiceId = getNodeId(expandedNode.Parent);
                    SPWebService webService = (SPWebService)SPFarm.Local.Services[new Guid(contentServiceId)];
                    SPApplicationPoolCollection applicationPoolCollection = webService.ApplicationPools;
                    addApplicationPools(applicationPoolCollection, expandedNode);
                }
                else if (kindOfNode(expandedNode).ToLower() == "webapplications")
                {
                    string contentServiceId = getNodeId(expandedNode.Parent);
                    SPWebService webService = (SPWebService)SPFarm.Local.Services[new Guid(contentServiceId)];
                    SPWebApplicationCollection webApplicationCollection = webService.WebApplications;
                    addWebApplications(webApplicationCollection, expandedNode);
                }
                else if (kindOfNode(expandedNode).ToLower() == "quotatemplates")
                {
                    string contentServiceId = getNodeId(expandedNode.Parent);
                    SPWebService webService = (SPWebService)SPFarm.Local.Services[new Guid(contentServiceId)];
                    SPQuotaTemplateCollection quotaTemplateCollection = webService.QuotaTemplates;
                    addQuotaTemplates(quotaTemplateCollection, expandedNode);

                }
                else if (kindOfNode(expandedNode).ToLower() == "properties")
                {
                    string contentServiceId = getNodeId(expandedNode.Parent);
                    SPWebService webService = (SPWebService)SPFarm.Local.Services[new Guid(contentServiceId)];
                    Hashtable propertiesTable = webService.Properties;
                    addProperties(propertiesTable, expandedNode);
                }
                else if (kindOfNode(expandedNode).ToLower() == "webapplication")
                {
                    SPFarm thisFarm = SPFarm.Local;
                    Guid webApplicationId = new Guid(getNodeId(expandedNode));
                    SPWebApplication webApplication = (SPWebApplication)thisFarm.GetObject(webApplicationId);
                    processWebApplication(webApplication, expandedNode);
                }
                else if (kindOfNode(expandedNode).ToLower() == "jobdefinitions")
                {
                    SPFarm thisFarm = SPFarm.Local;
                    Guid webApplicationId = new Guid(getWebAppId(expandedNode));
                    SPWebApplication webApplication = (SPWebApplication)thisFarm.GetObject(webApplicationId);
                    addJobDefinitions(webApplication.JobDefinitions, expandedNode);
                }
                else if (kindOfNode(expandedNode).ToLower() == "runningjobs")
                {
                    SPFarm thisFarm = SPFarm.Local;
                    Guid webApplicationId = new Guid(getWebAppId(expandedNode));
                    SPWebApplication webApplication = (SPWebApplication)thisFarm.GetObject(webApplicationId);
                    addRunningJobs(webApplication.RunningJobs, expandedNode);
                }
                else if (kindOfNode(expandedNode).ToLower() == "iissettings")
                {
                    SPFarm thisFarm = SPFarm.Local;
                    Guid webApplicationId = new Guid(getWebAppId(expandedNode));
                    SPWebApplication webApplication = (SPWebApplication)thisFarm.GetObject(webApplicationId);
                    addIisSettings(webApplication.IisSettings, expandedNode);
                }
                else if (kindOfNode(expandedNode).ToLower() == "webappfeatures")
                {
                    SPFarm thisFarm = SPFarm.Local;
                    Guid webApplicationId = new Guid(getWebAppId(expandedNode));
                    SPWebApplication webApplication = (SPWebApplication)thisFarm.GetObject(webApplicationId);
                    addFeatures(webApplication.Features, expandedNode);
                }
                else if (kindOfNode(expandedNode).ToLower() == "sitecolfeatures")
                {
                    Guid siteCollectionId = new Guid(getSiteCollectionId(expandedNode));
                    using (SPSite siteCollection = new SPSite(siteCollectionId))
                    {
                        addFeatures(siteCollection.Features, expandedNode);
                    }

                }
                else if (kindOfNode(expandedNode).ToLower() == "sitecollections")
                {
                    SPFarm thisFarm = SPFarm.Local;
                    Guid webApplicationId = new Guid(getWebAppId(expandedNode));
                    SPWebApplication webApplication = (SPWebApplication)thisFarm.GetObject(webApplicationId);
                    addSiteCollections(webApplication.Sites, expandedNode);
                }
                else if (kindOfNode(expandedNode).ToLower() == "sitecollection")
                {
                    Guid siteCollectionId = new Guid(getNodeId(expandedNode));
                    using (SPSite siteCollection = new SPSite(siteCollectionId))
                    {
                        processSiteCollection(siteCollection, expandedNode);
                    }

                }
                else if (kindOfNode(expandedNode).ToLower() == "sitecolrecyclebin")
                {
                    Guid siteCollectionId = new Guid(getSiteCollectionId(expandedNode));
                    using (SPSite siteCollection = new SPSite(siteCollectionId))
                    {
                        addRecycleBin(siteCollection.RecycleBin, expandedNode);
                    }

                }
                else if (kindOfNode(expandedNode).ToLower() == "solutions")
                {
                    Guid siteCollectionId = new Guid(getSiteCollectionId(expandedNode));
                    using (SPSite siteCollection = new SPSite(siteCollectionId))
                    {
                        addSolutions(siteCollection.Solutions, expandedNode);
                    }

                }
                else if (kindOfNode(expandedNode).ToLower() == "site")
                {
                    Guid siteCollectionId = new Guid(getSiteCollectionId(expandedNode));
                    Guid siteId = new Guid(getSiteId(expandedNode));
                    using (SPSite siteCollection = new SPSite(siteCollectionId))
                    {
                        using (SPWeb site = siteCollection.OpenWeb(siteId))
                        {
                            ProcessSite(site, expandedNode);
                        }
                    }
                }
                else if (kindOfNode(expandedNode).ToLower() == "sitecontenttypes")
                {
                    Guid siteCollectionId = new Guid(getSiteCollectionId(expandedNode));
                    Guid siteId = new Guid(getSiteId(expandedNode));
                    using (SPSite siteCollection = new SPSite(siteCollectionId))
                    {
                        using (SPWeb site = siteCollection.OpenWeb(siteId))
                        {
                            addContentTypes(site.ContentTypes, expandedNode);
                        }
                    }
                }
                else if (kindOfNode(expandedNode).ToLower() == "contenttypefields")
                {
                    Guid siteCollectionId = new Guid(getSiteCollectionId(expandedNode));
                    Guid siteId = new Guid(getSiteId(expandedNode));
                    SPContentTypeId contentTypeId = new SPContentTypeId(getContentTypeId(expandedNode));
                    using (SPSite siteCollection = new SPSite(siteCollectionId))
                    {
                        using (SPWeb site = siteCollection.OpenWeb(siteId))
                        {
                            SPContentType contentType = site.ContentTypes[contentTypeId];
                            addContentTypeFields(contentType.Fields, expandedNode);
                        }
                    }
                }
                else if (kindOfNode(expandedNode).ToLower() == "workflows")
                {
                    Guid siteCollectionId = new Guid(getSiteCollectionId(expandedNode));
                    Guid siteId = new Guid(getSiteId(expandedNode));
                    SPContentTypeId contentTypeId = new SPContentTypeId(getContentTypeId(expandedNode));
                    using (SPSite siteCollection = new SPSite(siteCollectionId))
                    {
                        using (SPWeb site = siteCollection.OpenWeb(siteId))
                        {
                            SPContentType contentType = site.ContentTypes[contentTypeId];
                            addWorkFlowAssociations(contentType.WorkflowAssociations, expandedNode);
                        }
                    }
                }
                else if (kindOfNode(expandedNode).ToLower() == "siteeventreceivers")
                {
                    Guid siteCollectionId = new Guid(getSiteCollectionId(expandedNode));
                    Guid siteId = new Guid(getSiteId(expandedNode));

                    using (SPSite siteCollection = new SPSite(siteCollectionId))
                    {
                        using (SPWeb site = siteCollection.OpenWeb(siteId))
                        {
                            addEventReceivers(site.EventReceivers, expandedNode);
                        }
                    }
                }
                else if (kindOfNode(expandedNode).ToLower() == "siteproperties")
                {
                    Guid siteCollectionId = new Guid(getSiteCollectionId(expandedNode));
                    Guid siteId = new Guid(getSiteId(expandedNode));

                    using (SPSite siteCollection = new SPSite(siteCollectionId))
                    {
                        using (SPWeb site = siteCollection.OpenWeb(siteId))
                        {
                            addProperties(site.AllProperties, expandedNode);
                        }
                    }
                }
                else if (kindOfNode(expandedNode).ToLower() == "sitefeatures")
                {
                    Guid siteCollectionId = new Guid(getSiteCollectionId(expandedNode));
                    Guid siteId = new Guid(getSiteId(expandedNode));

                    using (SPSite siteCollection = new SPSite(siteCollectionId))
                    {
                        using (SPWeb site = siteCollection.OpenWeb(siteId))
                        {
                            addFeatures(site.Features, expandedNode);
                        }
                    }
                }
                else if (kindOfNode(expandedNode).ToLower() == "sitefields")
                {
                    Guid siteCollectionId = new Guid(getSiteCollectionId(expandedNode));
                    Guid siteId = new Guid(getSiteId(expandedNode));

                    using (SPSite siteCollection = new SPSite(siteCollectionId))
                    {
                        using (SPWeb site = siteCollection.OpenWeb(siteId))
                        {
                            addSiteFields(site.Fields, expandedNode);
                        }
                    }
                }
                else if (kindOfNode(expandedNode).ToLower() == "users")
                {
                    Guid siteCollectionId = new Guid(getSiteCollectionId(expandedNode));
                    Guid siteId = new Guid(getSiteId(expandedNode));

                    using (SPSite siteCollection = new SPSite(siteCollectionId))
                    {
                        using (SPWeb site = siteCollection.OpenWeb(siteId))
                        {
                            addUsers(site.Users, expandedNode);
                        }
                    }
                }
                else if (kindOfNode(expandedNode).ToLower() == "listtemplates")
                {
                    Guid siteCollectionId = new Guid(getSiteCollectionId(expandedNode));
                    Guid siteId = new Guid(getSiteId(expandedNode));

                    using (SPSite siteCollection = new SPSite(siteCollectionId))
                    {
                        using (SPWeb site = siteCollection.OpenWeb(siteId))
                        {
                            addListTemplates(site.ListTemplates, expandedNode);
                        }
                    }
                }
                else if (kindOfNode(expandedNode).ToLower() == "siterecyclebin")
                {
                    Guid siteCollectionId = new Guid(getSiteCollectionId(expandedNode));
                    Guid siteId = new Guid(getSiteId(expandedNode));

                    using (SPSite siteCollection = new SPSite(siteCollectionId))
                    {
                        using (SPWeb site = siteCollection.OpenWeb(siteId))
                        {
                            addRecycleBin(site.RecycleBin, expandedNode);
                        }
                    }
                }
                else if (kindOfNode(expandedNode).ToLower() == "siterecyclebin")
                {
                    Guid siteCollectionId = new Guid(getSiteCollectionId(expandedNode));
                    Guid siteId = new Guid(getSiteId(expandedNode));

                    using (SPSite siteCollection = new SPSite(siteCollectionId))
                    {
                        using (SPWeb site = siteCollection.OpenWeb(siteId))
                        {
                            addRecycleBin(site.RecycleBin, expandedNode);
                        }
                    }
                }
                else if (kindOfNode(expandedNode).ToLower() == "webs")
                {
                    Guid siteCollectionId = new Guid(getSiteCollectionId(expandedNode));
                    Guid siteId = new Guid(getSiteId(expandedNode));

                    using (SPSite siteCollection = new SPSite(siteCollectionId))
                    {
                        using (SPWeb site = siteCollection.OpenWeb(siteId))
                        {
                            addWebs(site.Webs, expandedNode);
                        }
                    }
                }
                else if (kindOfNode(expandedNode).ToLower() == "lists")
                {
                    Guid siteCollectionId = new Guid(getSiteCollectionId(expandedNode));
                    Guid siteId = new Guid(getSiteId(expandedNode));

                    using (SPSite siteCollection = new SPSite(siteCollectionId))
                    {
                        using (SPWeb site = siteCollection.OpenWeb(siteId))
                        {

                            addLists(site.Lists, expandedNode);
                        }
                    }
                }
                else if (kindOfNode(expandedNode).ToLower() == "listviews")
                {
                    Guid siteCollectionId = new Guid(getSiteCollectionId(expandedNode));
                    Guid siteId = new Guid(getSiteId(expandedNode));
                    Guid listId = new Guid(getListId(expandedNode));
                    using (SPSite siteCollection = new SPSite(siteCollectionId))
                    {
                        using (SPWeb site = siteCollection.OpenWeb(siteId))
                        {
                            SPList list = site.Lists[listId];
                            addViews(list.Views, expandedNode);
                        }
                    }
                }
                else if (kindOfNode(expandedNode).ToLower() == "listcontenttypes")
                {
                    Guid siteCollectionId = new Guid(getSiteCollectionId(expandedNode));
                    Guid siteId = new Guid(getSiteId(expandedNode));
                    Guid listId = new Guid(getListId(expandedNode));
                    using (SPSite siteCollection = new SPSite(siteCollectionId))
                    {
                        using (SPWeb site = siteCollection.OpenWeb(siteId))
                        {
                            SPList list = site.Lists[listId];
                            addContentTypes(list.ContentTypes, expandedNode);
                        }
                    }
                }
                else if (kindOfNode(expandedNode).ToLower() == "listeventreceivers")
                {
                    Guid siteCollectionId = new Guid(getSiteCollectionId(expandedNode));
                    Guid siteId = new Guid(getSiteId(expandedNode));
                    Guid listId = new Guid(getListId(expandedNode));
                    using (SPSite siteCollection = new SPSite(siteCollectionId))
                    {
                        using (SPWeb site = siteCollection.OpenWeb(siteId))
                        {
                            SPList list = site.Lists[listId];
                            addEventReceivers(list.EventReceivers, expandedNode);
                        }
                    }
                }
                else if (kindOfNode(expandedNode).ToLower() == "listitems")
                {
                    Guid siteCollectionId = new Guid(getSiteCollectionId(expandedNode));
                    Guid siteId = new Guid(getSiteId(expandedNode));
                    Guid listId = new Guid(getListId(expandedNode));
                    using (SPSite siteCollection = new SPSite(siteCollectionId))
                    {
                        using (SPWeb site = siteCollection.OpenWeb(siteId))
                        {
                            SPList list = site.Lists[listId];
                            addlistItems(list.Items, expandedNode);
                            addFolder(list.RootFolder, expandedNode);
                        }
                    }
                }
                else if (kindOfNode(expandedNode).ToLower() == "listworkflows")
                {
                    Guid siteCollectionId = new Guid(getSiteCollectionId(expandedNode));
                    Guid siteId = new Guid(getSiteId(expandedNode));
                    Guid listId = new Guid(getListId(expandedNode));
                    using (SPSite siteCollection = new SPSite(siteCollectionId))
                    {
                        using (SPWeb site = siteCollection.OpenWeb(siteId))
                        {
                            SPList list = site.Lists[listId];
                            addWorkFlowAssociations(list.WorkflowAssociations, expandedNode);
                        }
                    }
                }
                else if (kindOfNode(expandedNode).ToLower() == "listforms")
                {
                    Guid siteCollectionId = new Guid(getSiteCollectionId(expandedNode));
                    Guid siteId = new Guid(getSiteId(expandedNode));
                    Guid listId = new Guid(getListId(expandedNode));
                    using (SPSite siteCollection = new SPSite(siteCollectionId))
                    {
                        using (SPWeb site = siteCollection.OpenWeb(siteId))
                        {
                            SPList list = site.Lists[listId];
                            addForms(list.Forms, expandedNode);
                        }
                    }
                }
                else if (kindOfNode(expandedNode).ToLower() == "listfields")
                {
                    Guid siteCollectionId = new Guid(getSiteCollectionId(expandedNode));
                    Guid siteId = new Guid(getSiteId(expandedNode));
                    Guid listId = new Guid(getListId(expandedNode));
                    using (SPSite siteCollection = new SPSite(siteCollectionId))
                    {
                        using (SPWeb site = siteCollection.OpenWeb(siteId))
                        {
                            SPList list = site.Lists[listId];
                            addListFields(list.Fields, expandedNode);
                        }
                    }
                }
                else if (kindOfNode(expandedNode).ToLower() == "folder")
                {
                    Guid siteCollectionId = new Guid(getSiteCollectionId(expandedNode));
                    Guid siteId = new Guid(getSiteId(expandedNode));
                    string folderId = getNodeId(expandedNode);
                    using (SPSite siteCollection = new SPSite(siteCollectionId))
                    {
                        using (SPWeb site = siteCollection.OpenWeb(siteId))
                        {
                            SPFolder folder = site.GetFolder(new Guid(folderId));
                            addFolder(folder, expandedNode);
                        }
                    }
                }
                else if (kindOfNode(expandedNode).ToLower() == "folderproperties")
                {
                    Guid siteCollectionId = new Guid(getSiteCollectionId(expandedNode));
                    Guid siteId = new Guid(getSiteId(expandedNode));
                    string folderId = getNodeId(expandedNode.Parent);
                    using (SPSite siteCollection = new SPSite(siteCollectionId))
                    {
                        using (SPWeb site = siteCollection.OpenWeb(siteId))
                        {
                            SPFolder folder = site.GetFolder(new Guid(folderId));
                            addProperties(folder.Properties, expandedNode);
                        }
                    }
                }
                else if (kindOfNode(expandedNode).ToLower() == "Fileproperties")
                {
                    Guid siteCollectionId = new Guid(getSiteCollectionId(expandedNode));
                    Guid siteId = new Guid(getSiteId(expandedNode));
                    string folderId = getNodeId(expandedNode.Parent.Parent);
                    string FileId = getNodeId(expandedNode.Parent);
                    using (SPSite siteCollection = new SPSite(siteCollectionId))
                    {
                        using (SPWeb site = siteCollection.OpenWeb(siteId))
                        {
                            SPFolder folder = site.GetFolder(new Guid(folderId));
                            SPFile File = folder.Files[FileId];
                            addProperties(File.Properties, expandedNode);
                        }
                    }
                }
                else if (kindOfNode(expandedNode).ToLower() == "Fileeventreceivers")
                {
                    Guid siteCollectionId = new Guid(getSiteCollectionId(expandedNode));
                    Guid siteId = new Guid(getSiteId(expandedNode));
                    string folderId = getNodeId(expandedNode.Parent.Parent);
                    string FileId = getNodeId(expandedNode.Parent);
                    using (SPSite siteCollection = new SPSite(siteCollectionId))
                    {
                        using (SPWeb site = siteCollection.OpenWeb(siteId))
                        {
                            SPFolder folder = site.GetFolder(new Guid(folderId));
                            SPFile File = folder.Files[FileId];
                            addEventReceivers(File.EventReceivers, expandedNode);
                        }
                    }

                }
            }
        }
    }
}
